package com.sec.hibernate;

import org.hibernate.*;
import org.hibernate.engine.spi.FilterDefinition;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.metadata.CollectionMetadata;
import org.hibernate.stat.Statistics;

import javax.naming.NamingException;
import javax.naming.Reference;
import java.io.Serializable;
import java.sql.Connection;
import java.util.Map;
import java.util.Set;

/**
 * 实现类
 * Created by Administrator on 2017/11/2.
 */
public class DynamicSessionFactoryImpl  implements DynamicSessionFactory{

    private Map<Object, SessionFactory> targetSessionFactorys;
    private SessionFactory defaultTargetSessionFactory;

    public SessionFactory getHibernateSessionFactory() {
        SessionFactory targetSessionFactory = targetSessionFactorys.get(CustomerContextHolder.getCustomerType());
        if (targetSessionFactory != null) {
            return targetSessionFactory;
        } else if (defaultTargetSessionFactory != null) {
            return defaultTargetSessionFactory;
        }
        return null;
    }

    public void setTargetSessionFactorys(
            Map<Object, SessionFactory> targetSessionFactorys) {
        this.targetSessionFactorys = targetSessionFactorys;
    }

    public void setDefaultTargetSessionFactory(
            SessionFactory defaultTargetSessionFactory) {
        this.defaultTargetSessionFactory = defaultTargetSessionFactory;
    }

    public SessionFactoryOptions getSessionFactoryOptions() {
        return getHibernateSessionFactory().getSessionFactoryOptions();
    }

    public SessionBuilder withOptions() {
        return getHibernateSessionFactory().withOptions();
    }

    public Session openSession() throws HibernateException {
        return getHibernateSessionFactory().openSession();
    }

    public Session getCurrentSession() throws HibernateException {
        return getHibernateSessionFactory().getCurrentSession();
    }

    public StatelessSessionBuilder withStatelessOptions() {
        return getHibernateSessionFactory().withStatelessOptions();
    }

    public StatelessSession openStatelessSession() {
        return getHibernateSessionFactory().openStatelessSession();
    }

    public StatelessSession openStatelessSession(Connection connection) {
        return getHibernateSessionFactory().openStatelessSession(connection);
    }

    public ClassMetadata getClassMetadata(Class aClass) {
        return getHibernateSessionFactory().getClassMetadata(aClass);
    }

    public ClassMetadata getClassMetadata(String s) {
        return getHibernateSessionFactory().getClassMetadata(s);
    }

    public CollectionMetadata getCollectionMetadata(String s) {
        return getHibernateSessionFactory().getCollectionMetadata(s);
    }

    public Map<String, ClassMetadata> getAllClassMetadata() {
        return getHibernateSessionFactory().getAllClassMetadata();
    }

    public Map getAllCollectionMetadata() {
        return getHibernateSessionFactory().getAllCollectionMetadata();
    }

    public Statistics getStatistics() {
        return getHibernateSessionFactory().getStatistics();
    }

    public void close() throws HibernateException {
        getHibernateSessionFactory().close();
    }

    public boolean isClosed() {
        return getHibernateSessionFactory().isClosed();
    }

    public Cache getCache() {
        return getHibernateSessionFactory().getCache();
    }

    public void evict(Class aClass) throws HibernateException {
        getHibernateSessionFactory().evict(aClass);
    }

    public void evict(Class aClass, Serializable serializable) throws HibernateException {
        getHibernateSessionFactory().evict(aClass, serializable);
    }

    public void evictEntity(String s) throws HibernateException {
        getHibernateSessionFactory().evictEntity(s);
    }

    public void evictEntity(String s, Serializable serializable) throws HibernateException {
        getHibernateSessionFactory().evictEntity(s, serializable);
    }

    public void evictCollection(String s) throws HibernateException {
        getHibernateSessionFactory().evictCollection(s);
    }

    public void evictCollection(String s, Serializable serializable) throws HibernateException {
        getHibernateSessionFactory().evictCollection(s, serializable);
    }

    public void evictQueries(String s) throws HibernateException {
        getHibernateSessionFactory().evictQueries(s);
    }

    public void evictQueries() throws HibernateException {
        getHibernateSessionFactory().evictQueries();
    }

    public Set getDefinedFilterNames() {
        return getHibernateSessionFactory().getDefinedFilterNames();
    }

    public FilterDefinition getFilterDefinition(String s) throws HibernateException {
        return getHibernateSessionFactory().getFilterDefinition(s);
    }

    public boolean containsFetchProfileDefinition(String s) {
        return getHibernateSessionFactory()
                .containsFetchProfileDefinition(s);
    }

    public TypeHelper getTypeHelper() {
        return getHibernateSessionFactory().getTypeHelper();
    }

    public Reference getReference() throws NamingException {
        return getHibernateSessionFactory().getReference();
    }
}
